{extend name="base"}
{block name="body"}
<ol class="breadcrumb">
  <li><a href="#">首页</a></li>
  <li><a href="#">商品管理</a></li>
  <li class="active">商品分类</li>
</ol>
<div class="panel panel-default">
  <div class="panel-heading">
    <a href="{:url('add_admin')}" class="btn btn-info">添加管理员角色</a>
  </div>
  <table class="table table-bordered">
    <colgroup>
      <col style="width: 1%;">
      <col style="width: 5%;">
      <col style="width: 30%;">
      <col style="width: 20%;">
      <col style="width: 10%;">
      <col style="width: 10%;">
    </colgroup>
    <thead>
      <tr align="center">
        <th>id</th>
        <th>用户名</th>
        <th>管理员类型</th>
        <th>创建时间</th>
        <th>最后登录</th>
        <th>状态</th>
        <th colspan="3">操作</th>
      </tr>
    </thead>
    <tbody>
    	{foreach $adminUserList as $userInfo}
    	<tr>
    		<td>{$userInfo.id}</td>
    		<td>{$userInfo.username}</td>
    		<td>{$userInfo.level}</td>
    		<td>{$userInfo.regtime}</td>
    		<td>{$userInfo.lastlogin}</td>
    		<td data-name="status">{$userInfo.status}</td>
    		<td><a href="{:url('edit_admin','id='.$userInfo.id)}" class="btn btn-primary">编辑</a></td>
    		<td><a href="javascript:" class="btn btn-default" data-name="del" data-id="{$userInfo.id}">删除</a></td>
    		<td>
    			{switch name="userInfo.status"}
				    {case value="正常"}
						<a href="javascript:" class="btn btn-danger" data-name="status" data-id="{$userInfo.id}" data-state="true">禁用</a>
				    {/case}
				    {case value="禁用"}
						<a href="javascript:" class="btn btn-success" data-name="status" data-id="{$userInfo.id}" data-state="false">启用</a>
				    {/case}
				    {default /}default
				{/switch}    			
    		</td>
    	</tr>
    	{/foreach}
    </tbody>
  </table>
</div>
<script type="text/javascript">
	$('td>a[data-name=status]').on('click',function(){
		var uid=$(this).attr('data-id');
		var state=$(this).attr('data-state');
		var obj=$(this);
		$.ajax({
			url: '{:url("ajax_update")}',
			type: 'POST',
			dataType: 'json',
			data: {'id':uid,'state':state},
		})
		.done(function(r) {
      if(r.code=='200'){
        obj.parent('td').siblings('td[data-name=status]').html('禁用');
        obj.removeClass('btn-danger').addClass('btn-success').attr('data-state','flase').html('启用');
      }else {
        obj.parent('td').siblings('td[data-name=status]').html('正常');
        obj.removeClass('btn-success').addClass('btn-danger').attr('data-state','true').html('禁用');
      }
		})
		.fail(function() {
			console.log("error");
		})		
	});
  $('td>a[data-name=del]').on('click',function(){
    var uid=$(this).attr('data-id');
    var obj=$(this);
    if (confirm("你确定删除该管理员？")) {
        $.ajax({
          url: '{:url("ajax_del")}',
          type: 'POST',
          dataType: 'json',
          data: {'id':uid},
        })
        .done(function(r) {
          if(r=='ok'){
            obj.parents('tr').hide(200);
          }
        })
        .fail(function() {
          console.log("error");
        })        
    }else {
        return false;
    }
  })
</script>
{/block}